<?php

/**
* PageFactory.php
*
* Produce concrete page base on cmd query string
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/


require_once("config.php");
require_once("UserErrorHandler.class.php");
require_once("Page.class.php");


/**
* Base on 'cmd' (from query string, GET method) value get concrete Page
* @return Page
*/

function &getPage($defaultPage = '')
{
    trigger_error("ENTER getPage()", E_USER_NOTICE);
    $cmd = '';
    if ( isset($_GET['cmd']) && strlen($_GET['cmd']) )
    {
        $cmd = $_GET['cmd'];
    }
    else
    {
        if ( $defaultPage =='' )
        {
            trigger_error("Default command not set", E_USER_ERROR);     
            $page = new Page;
            return $page;
        }
        trigger_error("Use default command: ".$defaultPage , E_USER_NOTICE);    
        $cmd = $defaultPage;
    }


    if ( file_exists ( basename($cmd.'.class.php') ) && is_file( basename($cmd.'.class.php') ) )
    {

        require_once basename($cmd.'.class.php');

        if ( class_exists( $cmd ) )
        {
            $page = new $cmd;
        }
        else
        {
            trigger_error("Command not recognized : ".$cmd , E_USER_ERROR);    
        }

    }
    else
    {
        trigger_error("Command not found : ".$cmd , E_USER_ERROR);    
    }


    trigger_error("LEAVE getPage()", E_USER_NOTICE);

    return $page;
}

?>
